<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-16
 * Time: 17:48
 */

namespace App\Http\Controllers\Account;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\SystemTypeVail;
use App\Model\Account\SystemType;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class SystemTypeController extends Controller
{
    //列表
    public function listType(Request $request)
    {
        $params = $request->all();
        $vali = SystemTypeVail::listType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $where = [];
        if ($params['st_id']){
            $where[] = ['st_superior_id', '=', $params['st_id']];
            //类型名字
            if ($request->input('st_name')){
                $where[] = ['st_name', 'like', "%".$params['st_name']."%"];
            }
            $list = SystemType::listType($where, env('PAGE_LIMIT_ADMIN'));
        } else {
            $list = false;
        }
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    /**添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addType(Request $request)
    {
        $params = $request->all();
        $vali = SystemTypeVail::addType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = SystemType::addType($params);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功！');
    }

    /**修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editType(Request $request)
    {
        $params = $request->all();
        $vali = SystemTypeVail::editType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = SystemType::editType($params);
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功！');
    }
    //删除分类
    public function delType(Request $request)
    {
        $params = $request->all();
        $vali = SystemTypeVail::delType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = SystemType::delType($params['st_id']);
        if ($res === -1) {
            return $this->error(Error::ERROR_DEL_HAVEDETAIL);
        }
        if (!$res) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功！');
    }

    /**
     * 根据上级ID  获取该分类的下拉框
     */
    public function getDropDownBox(Request $request)
    {
        $params = $request->all();
        $vali = SystemTypeVail::delType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $list = SystemType::storageRedis($params['st_id']);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

}